<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>供应商管理</title>
    <link rel="stylesheet" href="/layui/css/layui.css" media="all">
</head>
<body>
<div><button type="button" class="layui-btn addsupplier">添加甲方</button></div>
<table id="supplier" lay-filter="supplier"></table>
<script type="text/html" id="actiona">
    <a class="layui-btn layui-btn-xs editsuppier" data-userid="@{{= d.user_id }}">编辑</a>
</script>
<script src="/js/jquery.min.js"></script>
<script src="/layui/layui.all.js"></script>
<script>
    layui.use('table', function(){
        var table = layui.table;

        //第一个实例
        table.render({
            elem: '#supplier'
            ,height: 312
            ,url: '/admin/suppliers/list' //数据接口
            ,page: true //开启分页
            ,cols: [[ //表头
                {field: 'user_name', title: '甲方产品名称', fixed: 'left'}
                ,{field: 'user_id', title: '甲方产品标识'}
                ,{field: 'realname_supplier', title: '实名认证'}
                ,{field: 'phone_supplier', title: '手机认证'}
                ,{field: 'bank_supplier', title: '银行认证'}
                ,{field: 'base_score_supplier', title: '准入分认证'}
                ,{field: 'believe_score_supplier', title: '置信分认证'}
                ,{field: 'behavior_score_supplier', title: '行为分认证'}
                ,{field: 'action', title: '设置', toolbar: '#actiona'}
            ]]
            ,text: {
                none: '暂无数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
            }
            ,done: function(res, curr, count){
                $('.editsuppier').click(function(){
                    var user_id = $(this).attr('data-userid');
                    console.log(user_id);
                    //do something
                    layer.open({
                        type: 2,
                        content: '/admin/suppliers/edit?user_id='+user_id, //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                        area: ['60%', '70%'],
                        btn: ['确定', '取消'],
                        yes: function(index, layero){
                            var base_layero = $(layero).find("iframe").contents();
                            var user_name = base_layero.find("input[name=user_name]").val();
                            var realname_supplier = base_layero.find("select[name=realname_supplier]").val();
                            var phone_supplier = base_layero.find("select[name=phone_supplier]").val();
                            var bank_supplier = base_layero.find("select[name=bank_supplier]").val();
                            var base_score_supplier = base_layero.find("select[name=base_score_supplier]").val();
                            var believe_score_supplier = base_layero.find("select[name=believe_score_supplier]").val();
                            var behavior_score_supplier = base_layero.find("select[name=behavior_score_supplier]").val();
                            if(!user_id || !user_name){
                                layer.msg('用户信息不能为空');
                                return;
                            }
                            $.ajax({
                                url: '/admin/suppliers/edit',
                                type: 'post',
                                data: {
                                    user_id: user_id,
                                    user_name: user_name,
                                    realname_supplier: realname_supplier,
                                    phone_supplier: phone_supplier,
                                    bank_supplier: bank_supplier,
                                    base_score_supplier: base_score_supplier,
                                    believe_score_supplier: believe_score_supplier,
                                    behavior_score_supplier: behavior_score_supplier,
                                },
                                success: function(res){
                                    if(res.code == 0){
                                        layer.msg('修改成功');
                                        layer.close(index);
                                        location.href = location.href;
                                    }
                                }
                            });
                        }
                    });
                });
            }
        });
    });
</script>
<script>
    $('.addsupplier').click(function(){
        layer.open({
            type: 2,
            content: '/admin/suppliers/add', //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
            area: ['60%', '70%'],
            btn: ['确定', '取消'],
            yes: function(index, layero){
                var base_layero = $(layero).find("iframe").contents();
                var user_id = base_layero.find("input[name=user_id]").val();
                var user_name = base_layero.find("input[name=user_name]").val();
                var realname_supplier = base_layero.find("select[name=realname_supplier]").val();
                var phone_supplier = base_layero.find("select[name=phone_supplier]").val();
                var bank_supplier = base_layero.find("select[name=bank_supplier]").val();
                var base_score_supplier = base_layero.find("select[name=base_score_supplier]").val();
                var believe_score_supplier = base_layero.find("select[name=believe_score_supplier]").val();
                var behavior_score_supplier = base_layero.find("select[name=behavior_score_supplier]").val();
                if(!user_id || !user_name){
                    layer.msg('用户信息不能为空');
                    return;
                }
                $.ajax({
                    url: '/admin/suppliers/add',
                    type: 'post',
                    data: {
                        user_id: user_id,
                        user_name: user_name,
                        realname_supplier: realname_supplier,
                        phone_supplier: phone_supplier,
                        bank_supplier: bank_supplier,
                        base_score_supplier: base_score_supplier,
                        believe_score_supplier: believe_score_supplier,
                        behavior_score_supplier: behavior_score_supplier,
                    },
                    success: function(res){
                        if(res.code == 0){
                            layer.msg('添加成功');
                            layer.close(index);
                            location.href = location.href;
                        }
                    }
                });
            }
        });
    });
</script>
</body>
</html>